package com.xinnet.mms.webservice.custom.impl;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.beans.factory.annotation.Autowired;

import com.xinnet.mms.allot.service.CustomAllotService;
import com.xinnet.mms.common.AppGlobalContainer;
import com.xinnet.mms.customerManage.service.CustomerManageService;
import com.xinnet.mms.empcustomrelation.service.EmpCustomRelationService;
import com.xinnet.mms.entity.Customer;
import com.xinnet.mms.entity.CustomerRemark;
import com.xinnet.mms.entity.Emp;
import com.xinnet.mms.entity.EmpCustomRelation;
import com.xinnet.mms.utils.CustomerManage;
import com.xinnet.mms.webservice.custom.CustomInterface;

/**
 * 外部增加客户的接口
 * @author 王志军
 *
 */
@Path(value = "/custom")
public class CustomInterfaceImpl implements CustomInterface{

	@Autowired
	CustomAllotService customAllotService;
	@Autowired
	private CustomerManageService customerManageService;
	@Autowired
	private EmpCustomRelationService empCustomRelationService;
	SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	@POST
	@Path("/add")
	@Produces(MediaType.APPLICATION_XML)
	public String addCustom(@Context HttpServletRequest request,
			@Context HttpServletResponse response, @FormParam("cname") String cname, @FormParam("phone") String phone,
			@FormParam("regionalId") String regionalId, @FormParam("productLabel") String productLabel, @FormParam("remark") String remark) {
		
		
		Customer customer=new Customer();
		//封装数据
		customer.setRegionalId(regionalId);
		customer.setProductLabel(productLabel);
		customer.setCname(cname);
		if(phone.equals(""))
			phone="未填写";
		customer.setPhone(phone);
		String email="未填写";
		customer.setEmail(email);
		
		customer.setAddTime(sf.format(new Date()));
		customer.setCusId(UUID.randomUUID().toString());
		customer.setIsAllot(CustomerManage.ALLOT_NO);
		customer.setIsReDevel(CustomerManage.TWICE_NO);
		customer.setCustomerSource("400");
		customer.setPusherId("system");
		customer.setPushType(CustomerManage.PUSH_TYPE_PERSON);
		customer.setCustomerType(CustomerManage.NOT_REGISTER_NEW_CUSTOMER);
		Element root = DocumentHelper.createElement("result"); 
		Document document = DocumentHelper.createDocument(root);  
		//如果有相同的客户 则不进行分配
		try {
			if(customAllotService.getCheckCustomerByPhone(customer)!=null) {
				//out.print("表格中含有相同的客户");
				root.addAttribute("result", "表格中含有相同的客户");  
				return document.asXML();
			}
			//进行分配
			customAllotService.allot(customer, CustomerManage.NOT_REGISTER_NEW_CUSTOMER);
			if(AppGlobalContainer.is400AllotMap.get("controler")!=null && AppGlobalContainer.is400AllotMap.get("controler").equals("notAllot")){
				customAllotService.delCustomer(customer.getCusId());
				AppGlobalContainer.is400AllotMap.clear();
				//out.print("未添加成功，所在部门没有销售可以分配！");
				root.addAttribute("result", "未添加成功，所在部门没有销售可以分配！");
			} else {
				if(remark!=null||!"".equals(remark)){
					//添加remark
					EmpCustomRelation ecr = null;
					List<EmpCustomRelation>  ecrList = empCustomRelationService.getEmpCustomRelationByCusId(customer.getCusId());
					if(ecrList!=null&&!ecrList.isEmpty()){
						ecr = ecrList.get(0);
					}
					String empId = ecr.getEmpId();
					CustomerRemark customerRemark = new CustomerRemark();
					customerRemark.setId(UUID.randomUUID().toString());
					customerRemark.setCusId(customer.getCusId());
					customerRemark.setEmpId(empId);
					customerRemark.setRemark(remark);
					customerManageService.addCustomerRemark(customerRemark);
				}
				root.addAttribute("result", "OK");
			}
		} catch (SQLException e) {
			root.addAttribute("result", "未添加成功！");
			e.printStackTrace();
		} catch (Exception e) {
			root.addAttribute("result", "未添加成功！");
			e.printStackTrace();
		}
        System.out.print(document.asXML());
		return document.asXML();
	}
}
